package org.application.calculator.logic;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LineProcessor {
	
	private static Node convertNumericToSignedNode (String numeric) throws ParseException{
		Pattern pattern1 = Pattern.compile("\\(\\d+(\\.\\d+)?\\)");
		Matcher matcher1 = pattern1.matcher(numeric);
		Number nf;
		double d;
		if (matcher1.matches()){
			nf = NumberFormat.getInstance(Locale.UK).parse(numeric.replaceAll("\\(", "").replaceAll("\\)", ""));
			d = nf.doubleValue();
			Node son = new NumericNode(d);
	    	return new Negation(son);
		}else{
			nf = NumberFormat.getInstance(Locale.UK).parse(numeric);
			d = nf.doubleValue();
			return new NumericNode(d);
		}
	}
	
	public static Node createOperators(String str) throws DenominatorIsZeroException{
		Pattern pattern1 = Pattern.compile("\\-?\\d+(\\.\\d+)?");
		Pattern pattern2 = Pattern.compile("(\\(\\-\\d+(\\.\\d+)?\\)|\\d+(\\.\\d+)?)\\+(\\(\\-\\d+(\\.\\d+)?\\)|\\d+(\\.\\d+)?)");
		Pattern pattern3 = Pattern.compile("(\\(\\-\\d+(\\.\\d+)?\\)|\\d+(\\.\\d+)?)\\-(\\(\\-\\d+(\\.\\d+)?\\)|\\d+(\\.\\d+)?)");
		Pattern pattern4 = Pattern.compile("(\\(\\-\\d+(\\.\\d+)?\\)|\\d+(\\.\\d+)?)\\*(\\(\\-\\d+(\\.\\d+)?\\)|\\d+(\\.\\d+)?)");
		Pattern pattern5 = Pattern.compile("(\\(\\-\\d+(\\.\\d+)?\\)|\\d+(\\.\\d+)?)\\/(\\(\\-\\d+(\\.\\d+)?\\)|\\d+(\\.\\d+)?)");
		Matcher matcher1 = pattern1.matcher(str);
		Matcher matcher2 = pattern2.matcher(str);
		Matcher matcher3 = pattern3.matcher(str);
		Matcher matcher4 = pattern4.matcher(str);
		Matcher matcher5 = pattern5.matcher(str);
		
		String strWithoutTabs = str.replaceAll(" ", "");
		Node[] son = new Node[2];
		Node operation = null;
		
		if(matcher1.matches()){
			try{
			operation = LineProcessor.convertNumericToSignedNode(strWithoutTabs);
			}
			catch(ParseException ex){
				System.out.println("Numeric is not correct");
			}
		}
		
		if(matcher2.matches()){
			String[] members = strWithoutTabs.replaceAll("\\-", "").split("\\+");
			try{
			for (int i = 0; i < members.length; i++){
				son[i] = LineProcessor.convertNumericToSignedNode(members[i]);
			}
			}
			catch(ParseException ex){
				System.out.println("Numeric is not correct");
			}
			operation = new Adding(son[0],son[1]);
		}
		
		if(matcher3.matches()){
			String[] members = strWithoutTabs.replaceAll("\\(\\-", "(").split("\\-");
			try{
				for (int i = 0; i < members.length; i++){
					son[i] = LineProcessor.convertNumericToSignedNode(members[i]);
				}
				}
				catch(ParseException ex){
					System.out.println("Numeric is not correct");
				}
			operation = new Substraction(son[0],son[1]);
		}
		
		if(matcher4.matches()){
			String[] members = strWithoutTabs.replaceAll("\\-", "").split("\\*");
			try{
				for (int i = 0; i < members.length; i++){
					son[i] = LineProcessor.convertNumericToSignedNode(members[i]);
				}
				}
				catch(ParseException ex){
					System.out.println("Numeric is not correct");
				}
			operation = new Multiplication(son[0],son[1]);
		}
		
		if(matcher5.matches()){
			String[] members = strWithoutTabs.replaceAll("\\-", "").split("\\/");
			try{
				for (int i = 0; i < members.length; i++){
					son[i] = LineProcessor.convertNumericToSignedNode(members[i]);
				}
				if (son[1].evaluate() == 0){
					throw new DenominatorIsZeroException("The Denominator is 0");
				}
				}
				catch(ParseException ex){
					System.out.println("Numeric is not correct");
				}
			operation = new Division(son[0],son[1]);
		}
		
		if(!matcher1.matches() && !matcher2.matches() && !matcher3.matches() && !matcher4.matches() && !matcher5.matches()){
			System.out.println("Expression " + str + " is not correct!");
		}
		return operation;
	}
}
